package util;

public class PageList {
  
	public PageList(){}
	
	private int TotalItems = 0;   //总记录数
	
	private int CurrentPage = 1;  //当前页数
	private int PrePage = 0;      //上一页
	private int NeatPage = 0;     //下一页
	private int TotalPage = 0;    //总页数
	private int PageSize =20;     //每页的条数
	private String PageURL;
	
	private int Start = 0;
	
	public PageList(int totalItems){
		this.TotalItems = totalItems;
		
		
		CalculatePageNoInfo();
	}
	public PageList(int pageSise,int totalItems){
		this.TotalItems = totalItems;
		this.PageSize = pageSise;
		
		CalculatePageNoInfo();
	}
	public PageList(int page,int totalItems,int pagesize){
		this.TotalItems = totalItems;
		this.PageSize = pagesize;
	
		if(page<1){
			page = 1;
		}
		this.CurrentPage = page;
		CalculatePageNoInfo();
	}
	
	
	 /**
     * 计算页码相关信息 calculate
     */
	public void CalculatePageNoInfo() {
        //	1. 获取总条目数量
        this.TotalItems = getTotalItems();
        //	2. 计算页码信息
        //	2.1 总页数
        if (this.TotalItems == 0) {
            this.TotalPage = 0;
        } else {
            this.TotalPage = (this.TotalItems - 1) / this.PageSize + 1;
        }
        //	2.2 当前页，前页，后页
        if (this.TotalPage == 0) {
            this.CurrentPage = 0;
            this.PrePage = 0;
            this.NeatPage = 0;
            this.Start = 0;
        } else {
            this.CurrentPage = (this.CurrentPage > this.TotalPage ? this.TotalPage : this.CurrentPage);
            this.PrePage = ((this.CurrentPage - 1 < 1) ? 1 : (this.CurrentPage - 1));
            this.NeatPage = ((this.CurrentPage == this.TotalPage) ? this.CurrentPage : (this.CurrentPage + 1));
           
            this.Start = (this.CurrentPage - 1) * this.PageSize;
        }
    }
	
	public static String getnumpagelist(String To , String para , PageList Pagebean) {
        String stringresult = "";
        int pageno=Pagebean.getCurrentPage();
        int liststep = 10;//最多显示分页页数
        int pages = pageno<1?1:pageno;

        int pagescount = Pagebean.getTotalPage();//求总页数，ceil（num）取整不小于num

        if (pagescount < pages) {
            pages = pagescount;//如果分页变量大总页数，则将分页变量设计为总页数

        }
        if (pages < 1) {
            pages = 1;//如果分页变量小于１,则将分页变量设为１
        }
        
        int listbegin = (pages - (int) Math.ceil((double) liststep/2 ));//从第几页开始显示分页信息
        int listend=0;
        
        if (listbegin < 1) {
            listbegin = 1;
            listend = listbegin+10;
        }else
            listend = pages + liststep/2;//分页信息显示到第几页
        
        if (listend > pagescount) {
            listend = pagescount + 1;
        }

        //显示数据部分

        //<显示分页信息
        //<显示上一页
        
        if (pages > 1) {
            stringresult = "<a href=" + To + "?iPageNo=1&isPaging=Y&totalItem="+Pagebean.getTotalItems()+"&"+para+">first</a>";
            stringresult = stringresult + "   " + "<a href=" + To + "?iPageNo="+(pages-1)+"&isPaging=Y&totalItem="+Pagebean.getTotalItems()+"&"+para+">prev</a>";
        }//>显示上一页
        //<显示分页码

        for (int i = listbegin; i < listend; i++) {
            if (i != pages) {//如果i不等于当前页
                String link = To + "?iPageNo=" + i + "&isPaging=Y&totalItem="+Pagebean.getTotalItems()+"&"+para;
                stringresult = stringresult + "   " + "<a href=" + link + ">" + i + "</a>";
            } else {
                stringresult = stringresult + "   " + i;
            }
        }//显示分页码>
//<显示下一页

        if (pages != pagescount) {
            stringresult = stringresult + "   " + "<a href=" + To + "?iPageNo=" + (pages + 1) + "&isPaging=Y&totalItem="+Pagebean.getTotalItems()+"&"+para+">next</a>";
            stringresult=stringresult+"   <a href=" + To + "?iPageNo="+Pagebean.getTotalPage()+"&isPaging=Y&totalItem="+Pagebean.getTotalItems()+"&"+para+">last</a>";
        }//>显示下一页
//>显示分页信息

        if (Pagebean.getTotalPage() <= 1) {
            stringresult = "<div align='right'total" + Pagebean.getTotalPage() + "pages</span></div>";
        }else
            stringresult=stringresult+"   total " + Pagebean.getTotalPage() + "pages";
        
        return stringresult;
    }
	public int getTotalItems() {
		return TotalItems;
	}
	public void setTotalItems(int totalItems) {
		TotalItems = totalItems;
	}
	public int getCurrentPage() {
		return CurrentPage;
	}
	public void setCurrentPage(int currentPage) {
		CurrentPage = currentPage;
	}
	public int getPrePage() {
		return PrePage;
	}
	public void setPrePage(int prePage) {
		PrePage = prePage;
	}
	public int getNeatPage() {
		return NeatPage;
	}
	public void setNeatPage(int neatPage) {
		NeatPage = neatPage;
	}
	public int getTotalPage() {
		return TotalPage;
	}
	public void setTotalPage(int totalPage) {
		TotalPage = totalPage;
	}
	public int getPageSize() {
		return PageSize;
	}
	public void setPageSize(int pageSize) {
		PageSize = pageSize;
	}
	public String getPageURL() {
		return PageURL;
	}
	public void setPageURL(String pageURL) {
		PageURL = pageURL;
	}
	public int getStart() {
		return Start;
	}
	public void setStart(int start) {
		Start = start;
	}
}
